import java.awt.BorderLayout;
import java.awt.Desktop;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.SwingWorker;
import javax.swing.WindowConstants;

import org.apache.log4j.Logger;
import javax.swing.JTextField;
import java.awt.Font;
import java.awt.Color;

public class GSettings
    extends javax.swing.JFrame
{

    {
        // Set Look & Feel
        try
        {
            // Set Look & Feel
            try
            {
                String os = System.getProperty( "os.name" ).toLowerCase();
                if ( os.indexOf( "win" ) >= 0 )
                {
                    javax.swing.UIManager.setLookAndFeel( javax.swing.UIManager.getSystemLookAndFeelClassName() );
                }
                else
                {
                    javax.swing.UIManager.setLookAndFeel( "com.sun.java.swing.plaf.gtk.GTKLookAndFeel" );
                }
            }
            catch ( Exception e )
            {
                e.printStackTrace();
            }
        }
        catch ( Exception e )
        {
            e.printStackTrace();
        }

    }

    private JPanel contentPanel;
    private JButton buttonSave;
    private JComboBox comboLanguage;
    private JSeparator sepLanguage;
    private JLabel labelHeaderLanguage;
    private JSeparator sepSettings;
    private JLabel labelSettings;
    private JButton buttonCancel;
    private JPanel buttonPanel;
    GFileFinder fileFinder;
    Logger logger = Logger.getLogger( GSettings.class );
    SwingWorker<Boolean, Void> worker;
    Vector clipboardContents;
    Boolean keepTable = false;
    private JComboBox comboNumberQuickLinks;
    private JCheckBox checkBoxFullScreenFF;
    private JLabel lblDatumsformat;
    private JTextField txtDateFormat;
    private JButton button;
    JCheckBox checkBoxDragAndDrop;
    private JLabel lblWichtigEinigenderungen;
    private JCheckBox checkBoxHideWindow;

    /**
     * @wbp.parser.constructor
     */
    public GSettings( Boolean keepTable )
    {
        initGUI();
        setStoredSettingsInGUI();
        this.keepTable = keepTable;
    }

    public GSettings( GFileFinder fileFinder, Boolean keepTable )
    {
        this.fileFinder = fileFinder;
        this.keepTable = keepTable;
        initGUI();
        setStoredSettingsInGUI();
    }

    private void initGUI()
    {
        try
        {
            setDefaultCloseOperation( WindowConstants.DISPOSE_ON_CLOSE );
            this.setIconImage( new ImageIcon( getClass().getClassLoader().getResource( "Icons/Settings22x22.png" ) ).getImage() ); //$NON-NLS-1$
            this.setResizable( false );
            this.setTitle( CLanguage.getString( "GSettings.1" ) ); //$NON-NLS-1$
            this.setSize( 500, 378 );
            {
                buttonPanel = new JPanel();
                getContentPane().add( buttonPanel, BorderLayout.SOUTH );
                buttonPanel.setPreferredSize( new java.awt.Dimension( 632, 47 ) );
                buttonPanel.setBorder( BorderFactory.createTitledBorder( "" ) );
                GridBagLayout gbl_buttonPanel = new GridBagLayout();
                gbl_buttonPanel.columnWidths = new int[] { 226, 121, 129, 0 };
                gbl_buttonPanel.rowHeights = new int[] { 32, 0 };
                gbl_buttonPanel.columnWeights = new double[] { 0.0, 1.0, 0.0, Double.MIN_VALUE };
                gbl_buttonPanel.rowWeights = new double[] { 1.0, Double.MIN_VALUE };
                buttonPanel.setLayout( gbl_buttonPanel );
                {
                    buttonSave = new JButton();
                    GridBagConstraints gbc_buttonSave = new GridBagConstraints();
                    gbc_buttonSave.fill = GridBagConstraints.BOTH;
                    gbc_buttonSave.insets = new Insets( 0, 0, 0, 5 );
                    gbc_buttonSave.gridx = 1;
                    gbc_buttonSave.gridy = 0;
                    buttonPanel.add( buttonSave, gbc_buttonSave );
                    buttonSave.setText( CLanguage.getString( "GSettings.12" ) ); //$NON-NLS-1$
                    buttonSave.setIcon( new ImageIcon( getClass().getClassLoader().getResource( "Icons/Save22x22.png" ) ) );
                    buttonSave.addActionListener( new ActionListener()
                    {
                        public void actionPerformed( ActionEvent evt )
                        {
                            logger.debug( "buttonSave.actionPerformed, event=" + evt ); //$NON-NLS-1$

                            storeSettings();
                            closeWindow();

                            if ( fileFinder != null && fileFinder.isVisible() )
                            {
                                fileFinder.keepWindow = false;
                                Point p = fileFinder.getLocationOnScreen();
                                fileFinder.hideWindow();
                                GFileFinder fileFinder2 = new GFileFinder( true );
                                fileFinder2.setVisible( true );
                            }
                            else
                            {
                                fileFinder = null;
                            }

                        }
                    } );
                }
                {
                    buttonCancel = new JButton();
                    GridBagConstraints gbc_buttonCancel = new GridBagConstraints();
                    gbc_buttonCancel.fill = GridBagConstraints.BOTH;
                    gbc_buttonCancel.gridx = 2;
                    gbc_buttonCancel.gridy = 0;
                    buttonPanel.add( buttonCancel, gbc_buttonCancel );
                    buttonCancel.setText( CLanguage.getString( "GSettings.8" ) ); //$NON-NLS-1$
                    buttonCancel.setIcon( new ImageIcon(
                                                         getClass().getClassLoader().getResource( "Icons/cancel22x22.png" ) ) );
                    buttonCancel.addActionListener( new ActionListener()
                    {
                        public void actionPerformed( ActionEvent evt )
                        {
                            logger.debug( "buttonCancel.actionPerformed, event=" + evt ); //$NON-NLS-1$

                            if ( fileFinder != null )
                            {
                                fileFinder.keepWindow = false;
                            }
                            closeWindow();
                        }
                    } );
                }
            }
            {
                contentPanel = new JPanel();
                getContentPane().add( contentPanel, BorderLayout.CENTER );
                contentPanel.setBorder( BorderFactory.createTitledBorder( "" ) );
                contentPanel.setPreferredSize( new java.awt.Dimension( 632, 350 ) );
                GridBagLayout gbl_contentPanel = new GridBagLayout();
                gbl_contentPanel.columnWidths = new int[] { 468, 0, 0 };
                gbl_contentPanel.rowHeights = new int[] { 14, 0, 28, 40, 10, 30, 0, 0, 0, 0, 23, 0 };
                gbl_contentPanel.columnWeights = new double[] { 1.0, 0.0, Double.MIN_VALUE };
                gbl_contentPanel.rowWeights =
                    new double[] { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE };
                contentPanel.setLayout( gbl_contentPanel );
                {
                    labelHeaderLanguage = new JLabel();
                    GridBagConstraints gbc_labelHeaderLanguage = new GridBagConstraints();
                    gbc_labelHeaderLanguage.anchor = GridBagConstraints.NORTHWEST;
                    gbc_labelHeaderLanguage.insets = new Insets( 0, 0, 5, 5 );
                    gbc_labelHeaderLanguage.gridx = 0;
                    gbc_labelHeaderLanguage.gridy = 0;
                    contentPanel.add( labelHeaderLanguage, gbc_labelHeaderLanguage );
                    labelHeaderLanguage.setText( CLanguage.getString( "GSettings.10" ) );
                    labelHeaderLanguage.setFont( new java.awt.Font( "Tahoma", 1, 11 ) );
                }
                comboLanguage = new JComboBox();
                ComboBoxModel comboLanguageModel = new DefaultComboBoxModel( new String[] { "English", "Deutsch" } );
                {
                    sepLanguage = new JSeparator();
                    GridBagConstraints gbc_sepLanguage = new GridBagConstraints();
                    gbc_sepLanguage.gridwidth = 2;
                    gbc_sepLanguage.anchor = GridBagConstraints.NORTH;
                    gbc_sepLanguage.fill = GridBagConstraints.HORIZONTAL;
                    gbc_sepLanguage.insets = new Insets( 0, 0, 5, 0 );
                    gbc_sepLanguage.gridx = 0;
                    gbc_sepLanguage.gridy = 1;
                    contentPanel.add( sepLanguage, gbc_sepLanguage );
                }
                GridBagConstraints gbc_comboLanguage = new GridBagConstraints();
                gbc_comboLanguage.gridwidth = 2;
                gbc_comboLanguage.fill = GridBagConstraints.HORIZONTAL;
                gbc_comboLanguage.anchor = GridBagConstraints.SOUTH;
                gbc_comboLanguage.insets = new Insets( 0, 0, 5, 0 );
                gbc_comboLanguage.gridx = 0;
                gbc_comboLanguage.gridy = 2;
                contentPanel.add( comboLanguage, gbc_comboLanguage );
                comboLanguage.setModel( comboLanguageModel );
                {
                    labelSettings = new JLabel();
                    GridBagConstraints gbc_labelSettings = new GridBagConstraints();
                    gbc_labelSettings.anchor = GridBagConstraints.SOUTHWEST;
                    gbc_labelSettings.insets = new Insets( 0, 0, 5, 5 );
                    gbc_labelSettings.gridx = 0;
                    gbc_labelSettings.gridy = 3;
                    contentPanel.add( labelSettings, gbc_labelSettings );
                    labelSettings.setText( CLanguage.getString( "GSettings.11" ) );
                    labelSettings.setFont( new java.awt.Font( "SansSerif", 1, 12 ) ); //$NON-NLS-1$
                }
                {
                    sepSettings = new JSeparator();
                    GridBagConstraints gbc_sepSettings = new GridBagConstraints();
                    gbc_sepSettings.gridwidth = 2;
                    gbc_sepSettings.fill = GridBagConstraints.BOTH;
                    gbc_sepSettings.insets = new Insets( 0, 0, 5, 0 );
                    gbc_sepSettings.gridx = 0;
                    gbc_sepSettings.gridy = 4;
                    contentPanel.add( sepSettings, gbc_sepSettings );
                }
                SpinnerNumberModel number = new SpinnerNumberModel( 0, 0, 200, 1 );
                SpinnerNumberModel numberQuick = new SpinnerNumberModel( 0, 0, 30, 1 );
                {
                    lblDatumsformat = new JLabel( CLanguage.getString( "GSettings.22" ) );
                    GridBagConstraints gbc_lblDatumsformat = new GridBagConstraints();
                    gbc_lblDatumsformat.anchor = GridBagConstraints.SOUTHWEST;
                    gbc_lblDatumsformat.insets = new Insets( 0, 0, 5, 5 );
                    gbc_lblDatumsformat.gridx = 0;
                    gbc_lblDatumsformat.gridy = 5;
                    contentPanel.add( lblDatumsformat, gbc_lblDatumsformat );
                }
                {
                    txtDateFormat = new JTextField();
                    GridBagConstraints gbc_txtDateFormat = new GridBagConstraints();
                    gbc_txtDateFormat.insets = new Insets( 0, 0, 5, 5 );
                    gbc_txtDateFormat.fill = GridBagConstraints.HORIZONTAL;
                    gbc_txtDateFormat.gridx = 0;
                    gbc_txtDateFormat.gridy = 6;
                    contentPanel.add( txtDateFormat, gbc_txtDateFormat );
                    txtDateFormat.setColumns( 10 );
                }
                {
                    button = new JButton( "" );
                    button.addActionListener( new ActionListener()
                    {
                        public void actionPerformed( ActionEvent e )
                        {

                            openWebLink( "http://download.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html" );

                        }
                    } );
                    button.setIcon( new ImageIcon( GSettings.class.getResource( "/Icons/Help16x16.png" ) ) );
                    GridBagConstraints gbc_button = new GridBagConstraints();
                    gbc_button.insets = new Insets( 0, 0, 5, 0 );
                    gbc_button.gridx = 1;
                    gbc_button.gridy = 6;
                    contentPanel.add( button, gbc_button );
                }
                {
                    checkBoxFullScreenFF = new JCheckBox( CLanguage.getString( "GSettings.21" ) );
                    GridBagConstraints gbc_checkBoxFullScreenFF = new GridBagConstraints();
                    gbc_checkBoxFullScreenFF.insets = new Insets( 0, 0, 5, 5 );
                    gbc_checkBoxFullScreenFF.anchor = GridBagConstraints.WEST;
                    gbc_checkBoxFullScreenFF.gridx = 0;
                    gbc_checkBoxFullScreenFF.gridy = 7;
                    contentPanel.add( checkBoxFullScreenFF, gbc_checkBoxFullScreenFF );
                }
                {
                    checkBoxHideWindow = new JCheckBox( CLanguage.getString( "GSettings.25" ) );
                    GridBagConstraints gbc_checkBoxHideWindow = new GridBagConstraints();
                    gbc_checkBoxHideWindow.anchor = GridBagConstraints.WEST;
                    gbc_checkBoxHideWindow.insets = new Insets( 0, 0, 5, 5 );
                    gbc_checkBoxHideWindow.gridx = 0;
                    gbc_checkBoxHideWindow.gridy = 8;
                    contentPanel.add( checkBoxHideWindow, gbc_checkBoxHideWindow );
                }
                {
                    checkBoxDragAndDrop = new JCheckBox( CLanguage.getString( "GSettings.23" ) );
                    GridBagConstraints gbc_checkBoxDragAndDrop = new GridBagConstraints();
                    gbc_checkBoxDragAndDrop.anchor = GridBagConstraints.WEST;
                    gbc_checkBoxDragAndDrop.insets = new Insets( 0, 0, 5, 5 );
                    gbc_checkBoxDragAndDrop.gridx = 0;
                    gbc_checkBoxDragAndDrop.gridy = 9;
                    contentPanel.add( checkBoxDragAndDrop, gbc_checkBoxDragAndDrop );
                }
                {
                    lblWichtigEinigenderungen = new JLabel( CLanguage.getString( "GSettings.24" ) );
                    lblWichtigEinigenderungen.setForeground( Color.RED );
                    lblWichtigEinigenderungen.setFont( new Font( "Tahoma", Font.BOLD, 11 ) );
                    GridBagConstraints gbc_lblWichtigEinigenderungen = new GridBagConstraints();
                    gbc_lblWichtigEinigenderungen.anchor = GridBagConstraints.SOUTHWEST;
                    gbc_lblWichtigEinigenderungen.gridwidth = 2;
                    gbc_lblWichtigEinigenderungen.gridx = 0;
                    gbc_lblWichtigEinigenderungen.gridy = 10;
                    contentPanel.add( lblWichtigEinigenderungen, gbc_lblWichtigEinigenderungen );
                    if ( CProperties.firstStart.equals( "true" ) )
                    {
                        lblWichtigEinigenderungen.setVisible( false );
                    }
                }

            }
        }
        catch ( Exception e )
        {
            // add your error handling code here
            e.printStackTrace();
        }
    }

    // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    /**
     * Loads the program settings from config file
     */
    // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

    private void setStoredSettingsInGUI()
    {
        CProperties properties = new CProperties();
        properties.loadSettings();

        if ( CProperties.language.equals( "english" ) )
        {
            comboLanguage.setSelectedIndex( 0 );
        }
        else if ( CProperties.language.equals( "german" ) )
        {
            comboLanguage.setSelectedIndex( 1 );
        }

        if ( CProperties.fullScreenFileFinder == true )
        {
            checkBoxFullScreenFF.setSelected( true );
        }
        else
        {
            checkBoxFullScreenFF.setSelected( false );
        }

        txtDateFormat.setText( CProperties.datePattern );

        if ( CProperties.DragAndDrop == true )
        {
            checkBoxDragAndDrop.setSelected( true );
        }
        else
        {
            checkBoxDragAndDrop.setSelected( false );
        }

        if ( CProperties.hideWindowOnFocusLost == true )
        {
            checkBoxHideWindow.setSelected( true );
        }
        else
        {
            checkBoxHideWindow.setSelected( false );
        }

    } // End of method

    // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    /**
     * Stores the program settings in config file
     */
    // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

    public void storeSettings()
    {
        CProperties prop = new CProperties();
        prop.loadSettings();

        CProperties.firstStart = "false";
        CProperties.fullScreenFileFinder = checkBoxFullScreenFF.isSelected();
        CProperties.DragAndDrop = checkBoxDragAndDrop.isSelected();
        CProperties.datePattern = txtDateFormat.getText().trim();
        CProperties.hideWindowOnFocusLost = checkBoxHideWindow.isSelected();

        int selection = comboLanguage.getSelectedIndex();

        switch ( selection )
        {
            case 0:
                CProperties.language = "english";
                break;
            case 1:
                CProperties.language = "german";
                break;
        }

        prop.updateProperties();

        if ( fileFinder != null && fileFinder.isVisible() )
        {
            fileFinder.hideWindow();

            GFileFinder fileFinder = new GFileFinder();
            fileFinder.setVisible( true );
            fileFinder.setLocationRelativeTo( null );
        }

    } // End of Method

    // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    /**
     * Opens a Link in the Default Web Browser
     */
    // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

    private void openWebLink( String weblink )
    {
        try
        {
            Desktop.getDesktop().browse( new URI( weblink ) );
        }
        catch ( IOException e1 )
        {
            System.out.println( e1 );
            e1.printStackTrace();
        }
        catch ( URISyntaxException e1 )
        {
            System.out.println( e1 );
            e1.printStackTrace();
        }
    }

    // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    /**
     * Closes this window by setting it invisible
     */
    // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

    private void closeWindow()
    {
        contentPanel.getParent().getParent().getParent().getParent().setVisible( false );

    } // End of Method

} // End of class
